package 极客算法训练营.chapter03;

import java.util.HashMap;
import java.util.Map;

public class 两数之和 {

    public static void main(String[] args) {

    }

    public int[] twoSum(int[] nums, int target) {
        int i = 0, j = 0;
        c:
        for (; i < nums.length - 1; i++) {
            for (j = i + 1; j < nums.length; j++) {
                if (nums[i] + nums[j] == target) break c;
            }
        }
        return new int[]{i, j};
    }

    public int[] twoSum2(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(target - nums[i])) {
                return new int[]{map.get(target - nums[i]), i};
            }
            map.put(nums[i], i);
        }
        return new int[0];
    }
}
